import request from '@/utils/request'

// 模拟数据
const mockData = {
  // 产品列表
  productList: [
    {
      id: 1,
      code: "PRD001",
      name: "管道阀门A型",
      categoryId: 1,
      categoryName: "管道阀门",
      description: "标准管道阀门A型号",
      createTime: "2023-10-01",
      specification: "DN100",
      material: "不锈钢304",
      grade: "A级",
      status: "停用"
    },
    {
      id: 2,
      code: "PRD002",
      name: "管道接头B型",
      categoryId: 2,
      categoryName: "管道接头",
      description: "标准管道接头B型号",
      createTime: "2023-11-01",
      specification: "DN80",
      material: "不锈钢316L",
      grade: "A级",
      status: "停用"
    },
    {
      id: 3,
      code: "PRD003",
      name: "管道连接器C型",
      categoryId: 3,
      categoryName: "管道连接器",
      description: "标准管道连接器C型号",
      createTime: "2023-12-01",
      specification: "DN50",
      material: "碳钢Q235",
      grade: "B级",
      status: "停用"
    }
  ],
  // 产品详情
  productDetail: {
    id: 1,
    code: "PRD001",
    name: "管道阀门A型",
    categoryId: 1,
    categoryName: "管道阀门",
    description: "标准管道阀门A型号",
    createTime: "2023-10-01",
    specification: "DN100",
    material: "不锈钢304",
    grade: "A级",
    status: "停用"
  }
};

// 使用模拟数据响应
const useMockData = false;

// 查询产品信息列表
export function listProduct(query) {
  if (useMockData) {
    console.log('Mock API: listProduct', query);
    
    // 使用模拟数据
    let filteredProducts = [...mockData.productList];
    
    // 应用筛选条件
    if (query) {
      if (query.name) {
        filteredProducts = filteredProducts.filter(product => 
          product.name.toLowerCase().includes(query.name.toLowerCase()));
      }
      if (query.code) {
        filteredProducts = filteredProducts.filter(product => 
          product.code.toLowerCase().includes(query.code.toLowerCase()));
      }
      if (query.categoryId) {
        filteredProducts = filteredProducts.filter(product => 
          product.categoryId === parseInt(query.categoryId));
      }
    }
    
    // 处理分页
    const pageSize = query.pageSize || 10;
    const pageNum = query.pageNum || 1;
    const startIndex = (pageNum - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const pagedProducts = filteredProducts.slice(startIndex, endIndex);
    
    return Promise.resolve({
      code: 200,
      rows: pagedProducts,
      total: filteredProducts.length
    });
  }
  
  return request({
    url: '/qep/product/list',
    method: 'get',
    params: query
  })
}

// 查询产品信息详细
export function getProduct(id) {
  if (useMockData) {
    console.log('Mock API: getProduct', id);
    const product = mockData.productList.find(p => p.id === parseInt(id));
    return Promise.resolve({
      code: 200,
      data: product || mockData.productDetail
    });
  }
  
  return request({
    url: '/qep/product/' + id,
    method: 'get'
  })
}

// 新增产品信息
export function addProduct(data) {
  if (useMockData) {
    console.log('Mock API: addProduct', data);
    // 创建新产品
    const newId = Math.max(...mockData.productList.map(p => p.id)) + 1;
    const newProduct = {
      ...data,
      id: newId,
      createTime: new Date().toISOString().split('T')[0]
    };
    
    mockData.productList.push(newProduct);
    
    return Promise.resolve({
      code: 200,
      msg: "操作成功",
      data: newProduct
    });
  }
  
  return request({
    url: '/qep/product',
    method: 'post',
    data: data
  })
}

// 修改产品信息
export function updateProduct(data) {
  if (useMockData) {
    console.log('Mock API: updateProduct', data);
    const index = mockData.productList.findIndex(p => p.id === data.id);
    if (index !== -1) {
      mockData.productList[index] = { ...mockData.productList[index], ...data };
    }
    
    return Promise.resolve({
      code: 200,
      msg: "操作成功"
    });
  }
  
  return request({
    url: '/qep/product',
    method: 'put',
    data: data
  })
}

// 删除产品信息
export function delProduct(id) {
  if (useMockData) {
    console.log('Mock API: delProduct', id);
    const index = mockData.productList.findIndex(p => p.id === parseInt(id));
    if (index !== -1) {
      mockData.productList.splice(index, 1);
    }
    
    return Promise.resolve({
      code: 200,
      msg: "操作成功"
    });
  }
  
  return request({
    url: '/qep/product/' + id,
    method: 'delete'
  })
} 